package com.ly.chatsimple.tcp.task

import com.ly.data.MessageProtobuf.Req
import com.ly.data.MessageProtobuf.TaskProto
import com.ly.data.MessageProtobuf.TaskType
import com.ly.nethub.task.BaseTask
import com.ly.nethub.utils.LogUtils

class RequestTask private constructor(private val param: Req) : BaseTask() {

    override fun buildRequest(): Any {
        val byteString = param.toByteString()
        return TaskProto.newBuilder()
            .setSeq(taskId)
            .setType(TaskType.Request)
            .setData(byteString)
            .setFrom("com.ly.chatsimple")
            .setVersion("v1.0")
            .build()
    }

    override fun isValidResponse(msg: Any?): Any? {
        if (msg == null) return null
        if (msg !is TaskProto) {
            LogUtils.d("$TAG 不是TaskProto $msg")
            return null
        }
        if (msg.seq == taskId && msg.type == TaskType.Response) return msg
        return null
    }

    companion object {
        private const val TAG = "RequestTask"
        fun create(param: Req): RequestTask = RequestTask(param)
    }
}